﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsAccessPoint
{
    public partial class frm_Main : Form
    {
        public frm_Main()
        {
            InitializeComponent();

            if (WindowsAccessPoint.Properties.Settings.Default.SSID != "")
            {
                txt_SSID.Text = WindowsAccessPoint.Properties.Settings.Default.SSID;
            }
            if (WindowsAccessPoint.Properties.Settings.Default.Password != null)
            {
                txt_Password.Text = WindowsAccessPoint.Properties.Settings.Default.Password;
            }
            if (Environment.GetCommandLineArgs().Length > 1)
            {
                foreach (string arg in Environment.GetCommandLineArgs())
                {
                    if (arg == "start")
                    {
                        StopWLAN();
                        SetWLAN(txt_SSID.Text, txt_Password.Text);
                        StartWLAN();
                    }
                    if (arg == "stop")
                    {
                        StopWLAN();
                    }
                    if (arg == "route")
                    {
                        System.Diagnostics.Process p = new System.Diagnostics.Process();
                        p.StartInfo.FileName = "route.exe";
                        p.StartInfo.Arguments = "ADD " + Environment.GetCommandLineArgs()[3] + " MASK " + Environment.GetCommandLineArgs()[4] + " " + Environment.GetCommandLineArgs()[5];
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.CreateNoWindow = true;
                        p.Start();
                        p.WaitForExit();
                        p.Close();
                    }
                }
                Environment.Exit(0);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StopWLAN();
            SetWLAN(txt_SSID.Text, txt_Password.Text);
            lbl_Status.Text = StartWLAN();
            Properties.Settings.Default.SSID = txt_SSID.Text;
            Properties.Settings.Default.Password = txt_Password.Text;
            Properties.Settings.Default.Save();
        }
        private string SetWLAN(string SSID, string key)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "netsh.exe";
            p.StartInfo.Arguments = "wlan set hostednetwork mode=allow ssid=\"" + SSID + "\" key=\""+ key +"\" keyUsage=persistent";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            string sOutput;
            p.Start();
            sOutput = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            return sOutput;
        }

        private string StartWLAN()
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "netsh.exe";
            p.StartInfo.Arguments = "wlan start hostednetwork";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            string sOutput;
            p.Start();
            sOutput = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            return sOutput;
        }

        private string StopWLAN()
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "netsh.exe";
            p.StartInfo.Arguments = "wlan stop hostednetwork";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.CreateNoWindow = true;
            string sOutput;
            p.Start();
            sOutput = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            return sOutput;
        }

        private void btn_stop_Click(object sender, EventArgs e)
        {
            lbl_Status.Text = StopWLAN();
        }
    }
}
